package Day26;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Demo03 {
    //XML解析网络数据
    public static void main(String[] args) throws IOException, DocumentException {
        String phone = "18181145205";
        //1.  获取XML资源的输入流
        URL url = new URL("http://apis.juhe.cn/mobile/get?phone="+phone+"&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253");
        URLConnection coon = url.openConnection();
        InputStream is = coon.getInputStream();

        //创建一个XML读取对象
        SAXReader sr = new SAXReader();
        // 通过读取对象 读取XML数据  并返回文档对象
        Document doc = sr.read(is);
        // 获取根节点
        Element root  = doc.getRootElement();
        // 解析内容
        String code = root.elementText("resultcode");
        if("200".equals(code)){
            Element result = root.element("result");//获取根节点的子对象
            String prov = result.elementText("province");//获取省份
            String city = result.elementText("city");//获取城市
            if(prov.equals(city)){
                System.out.println("手机号码归属地为：" + city);
            }else{
                System.out.println("手机号码归属地为:" + prov + " " + city);
            }
        }else{
            System.out.println("请输入正确的手机号");
        }
    }
}
